package ${package}.actions;

import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.token.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.bristleback.server.bristle.actions.AnnotatedRemoteAction;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.messages.SimpleConnectorMessageSender;
import pl.bristleback.server.bristle.utils.BristleTokenFactory;

@Controller
@AnnotatedRemoteAction(actionName = "reverseAction")
public class HelloWorldAction implements RemoteAction {

  @Autowired
  private SimpleConnectorMessageSender messageSender;

  public void performAction(WebSocketConnector connector, Token token) {
    String message = token.getString("value");
    String reversedMessage = new StringBuilder(message).reverse().toString();

    Token response = BristleTokenFactory.createResponseToken(token);
    response.setString("reversedValue", reversedMessage);    
    messageSender.sendMessage(response, connector);
  }
}